package controller

import (
	"digital/server/common"
	"digital/server/model"
	"digital/server/response"
	"fmt"
	"github.com/gin-gonic/gin"
	"log"
	"net/http"
	"time"
)

// GetAll 获取当前所有社团和活动信息并在首页轮播显示
func GetAll(ctx *gin.Context) {
	//获取十佳社团数据
	DB := common.GetDB()

	// 获取当前年份,查询符合条件的 Cid 列表
	currentYear := time.Now().Year()
	var outstandingClubs []model.OutstandingClubs
	DB.Where("YEAR(Otime) = ?", currentYear).Find(&outstandingClubs)

	// 获取这些 Cid 对应的社团信息
	var tenclubs []model.Club
	var cids []int
	for _, oc := range outstandingClubs {
		cids = append(cids, oc.Cid)
	}

	result := DB.Where("id IN (?)", cids).Find(&tenclubs)
	if result.Error != nil {
		response.Response(ctx, http.StatusBadRequest, 400, nil, "发生错误，请重试")
		log.Println("err:")
		return
	}
	print(result.Count)

	var tenimages []model.CImage
	err5 := DB.Where("Cid IN (?) AND Main=?", cids, 1).Find(&tenimages).Error
	if err5 != nil {
		response.Response(ctx, http.StatusBadRequest, 400, nil, "发生错误，请重试")
		log.Println("err:", err5)
		return
	}

	// 存放所有十佳社团的图片的路径
	var paths []string

	// 遍历 tenimages，提取每个 model.CImage 的 Path 字段并添加到 paths 数组中
	for _, img := range tenimages {
		paths = append(paths, img.Path)
	}

	//获取所有社团信息
	//DB1 := common.GetDB()
	//DB1 = DB1.Where("Exist=?", 1)

	db := common.GetDB()
	// 查询 clubs 表中 Exist 为 1 的记录
	var clubs []model.Club
	db.Where("Exist = ?", 1).Find(&clubs)

	// 用来存放满足条件的 Cid 的列表
	var cidList []int

	// 遍历 clubs，查询每个社团是否在 c_images 表中有 Main 为 1 的记录
	for _, club := range clubs {
		var count int64
		db.Model(&model.CImage{}).Where("Cid = ? AND Main = ?", club.Cid, 1).Count(&count)
		if count > 0 {
			cidList = append(cidList, club.Cid)
		}
	}

	fmt.Println("满足条件的 Cid 列表:", cidList)

	//获得所有有主图的社团
	var allClubs []model.Club
	err := DB.Table("clubs").Where("id IN (?)", cidList).Find(&allClubs).Error
	if err != nil {
		response.Response(ctx, http.StatusBadRequest, 400, nil, "发生错误，请重试")
		log.Println("err:", err)
		return
	}

	// 获取社团图片
	DB2 := common.GetDB()
	var tmpClub []string

	for _, clubid := range cidList {
		var i model.CImage
		err2 := DB2.Table("c_images").Where("Cid=? AND Main=?", clubid, 1).Find(&i).Error
		if err2 != nil {
			response.Response(ctx, http.StatusBadRequest, 400, nil, "发生错误，请重试")
			log.Println("err2:", err2)
			return
		}
		tmpClub = append(tmpClub, i.Path)
	}

	//获取所有活动信息
	DB3 := common.GetDB()
	DB3.Where("State IN (?)", []int{1, 2, 3})

	var activities []model.Activity
	err3 := DB3.Find(&activities).Error
	if err3 != nil {
		response.Response(ctx, http.StatusBadRequest, 400, nil, "发生错误，请重试")
		log.Println("err:", err3)
		return
	}

	//获取公告信息
	Db := common.GetDB()
	var notices []model.Notice

	// 查询并按照 CreatedAt 时间倒序排序
	err4 := Db.Order("Ntime DESC").Find(&notices).Error
	if err4 != nil {
		response.Response(ctx, http.StatusBadRequest, 400, nil, "发生错误，请重试")
		log.Println("err:", err4)
		return
	}

	// 返回查询结果
	response.Success(ctx, gin.H{"tenclubs": tenclubs, "imagesPath": paths, "allImage": tmpClub, "clubs": allClubs, "activities": activities, "notices": notices}, "获取成功")
}
